﻿package mortal.game.view.copy.petBreak
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.copy.petBreak.vo.*;
    import mortal.game.view.vip.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class PetBreakCopyModule extends BaseWindow
    {
        private var _bg:GImageBitmap;
        private var _pointSp:Sprite;
        private var _tabBar:GTabarNew;
        private var _petListComBo:GComboBox;
        private var _sweep:GTextFiled;
        private var _vit:GTextFiled;
        private var _addVitBtn:GLoadedButton;
        private var _effectPlayer:SWFPlayer;
        private var _openData:PetBreakCopyOpenData;
        private var _points:Vector.<PetBreakCopyPointItem>;

        public function PetBreakCopyModule(param1:ILayer = null)
        {
            this._points = new Vector.<PetBreakCopyPointItem>;
            super(param1);
            setSize(855, 580);
            title = Language.getString(21422);
            titleHeight = 60;
            return;
        }// end function

        private function onClickAddTodayTimeBtn(event:MouseEvent) : void
        {
            return;
        }// end function

        private function onClickAddVitBtn(event:MouseEvent) : void
        {
            var _loc_2:* = Cache.instance.copy.petBreakCopy.petBreakInfo;
            if (_loc_2.buyTimes >= 4)
            {
                MsgManager.showRollTipsMsg(Language.getString(70350));
                return;
            }
            var _loc_3:* = _loc_2.buyTimes + 1;
            var _loc_4:* = PetBreakCopyUtil.getHPCost(_loc_3);
            Alert.show(Language.getStringByParam(70346, _loc_3, _loc_4, 20), null, Alert.YES | Alert.NO, null, this.onClose);
            return;
        }// end function

        private function onClose(param1:int) : void
        {
            if (param1 == Alert.YES)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetBreakCopyBuyHP));
            }
            return;
        }// end function

        private function onTabBarChange(event:Event = null) : void
        {
            var _loc_2:* = this._tabBar.selectedIndex + 1;
            this._bg.imgUrl = "PetBreakLevel_" + _loc_2 + ".jpg";
            this.point = _loc_2;
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetBreakCopyInfoGet, _loc_2));
            return;
        }// end function

        private function set point(param1:int) : void
        {
            var _loc_2:PetBreakCopyPointItem = null;
            var _loc_3:TPetBreakConfig = null;
            var _loc_6:int = 0;
            this.cleanPoint(false);
            var _loc_4:* = Cache.instance.copy.petBreakCopy.petBreakInfo.floor;
            var _loc_5:* = Cache.instance.copy.petBreakCopy.petBreakInfo.level;
            var _loc_7:int = 0;
            while (_loc_7 < int.MAX_VALUE)
            {
                
                _loc_3 = PetBreakConfig.instance.getItem(param1, (_loc_7 + 1));
                if (!_loc_3)
                {
                    break;
                }
                if (this._points.length > _loc_7)
                {
                    _loc_2 = this._points[_loc_7];
                }
                else
                {
                    _loc_2 = UICompomentPool.getUICompoment(PetBreakCopyPointItem);
                    _loc_2.isHideDispose = true;
                    this._points.push(_loc_2);
                }
                if (_loc_3.isBig == 1)
                {
                    _loc_6++;
                }
                this._pointSp.addChild(_loc_2);
                _loc_2.setData(_loc_3, param1 + _loc_6.toString());
                _loc_2.isActivate = _loc_4 > param1 || _loc_4 == param1 && _loc_5 > _loc_7;
                _loc_7++;
            }
            this.addEffect();
            return;
        }// end function

        private function onEnter(event:DataEvent) : void
        {
            var _loc_2:* = event.data as PetBreakCopyOperVo;
            _loc_2.petUid = this._petListComBo.selectedIndex != -1 ? (this._petListComBo.selectedItem.value) : ("");
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetBreakCopyPointItemOper, _loc_2));
            return;
        }// end function

        private function onInfoReq(param1:Object) : void
        {
            var _loc_3:PetBreakCopyPointItem = null;
            var _loc_4:SPetBreakStarLevel = null;
            var _loc_2:* = param1 as Array;
            for each (_loc_4 in _loc_2)
            {
                
                _loc_3 = this._points[(_loc_4.level - 1)] as PetBreakCopyPointItem;
                _loc_3.maxStar = _loc_4.star;
            }
            return;
        }// end function

        private function onInfoUpdate(param1:Object = null) : void
        {
            var _loc_2:* = Cache.instance.copy.petBreakCopy.petBreakInfo;
            this._vit.text = _loc_2.hp + "/" + GameConst.PetBreakMaxHp;
            this._petListComBo.selectedIndex = PetBreakCopyUtil.maxPetIndex;
            this.setSweepTxt();
            return;
        }// end function

        private function onPetAddOrRemove(param1:Object) : void
        {
            this._petListComBo.dataProvider = PetBreakCopyUtil.petComboDp;
            this._petListComBo.drawNow();
            return;
        }// end function

        private function cleanPoint(param1:Boolean = true) : void
        {
            var _loc_2:PetBreakCopyPointItem = null;
            for each (_loc_2 in this._points)
            {
                
                _loc_2.dispose(param1);
            }
            if (param1)
            {
                this._points.splice(0, this._points.length);
            }
            return;
        }// end function

        private function addEffect() : void
        {
            var _loc_3:PetBreakCopyPointItem = null;
            var _loc_1:* = this._openData.type == 0 ? (Cache.instance.copy.petBreakCopy.petBreakInfo.floor) : (this._openData.floor);
            var _loc_2:* = this._openData.type == 0 ? (Cache.instance.copy.petBreakCopy.petBreakInfo.level) : (this._openData.level);
            if (_loc_1 != (this._tabBar.selectedIndex + 1))
            {
                DisplayUtil.removeMe(this._effectPlayer);
                return;
            }
            if (this._effectPlayer == null)
            {
                this._effectPlayer = UIFactory.getUICompoment(SWFPlayer);
            }
            if (_loc_2 < this._points.length)
            {
                _loc_3 = this._points[_loc_2] as PetBreakCopyPointItem;
                _loc_3.effectPlayer = this._effectPlayer;
                this._effectPlayer.load("petBreakCurPontEffect_" + this._openData.type + ".swf", ModelType.NormalSwf, null);
                this._effectPlayer.play();
                this._pointSp.addChild(this._effectPlayer);
            }
            return;
        }// end function

        public function openByIndex(param1:PetBreakCopyOpenData) : void
        {
            this._openData = param1;
            this._tabBar.selectedIndex = this._openData.index;
            this.onTabBarChange();
            return;
        }// end function

        public function setSweepTxt() : void
        {
            var _loc_1:* = Cache.instance.copy.petBreakCopy.petBreakInfo;
            var _loc_2:* = VipUtil.getVipAddStr(EVipRecordType._EVipRecordTypePetBreak);
            this._sweep.htmlText = Language.getStringByParam(70338, _loc_1.sweepTimes, PetBreakCopyUtil.maxSweepTimes) + _loc_2;
            return;
        }// end function

        private function onResCompl() : void
        {
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.PetBreakTxt_1, 35, 550, this));
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.PetBreakTxt_2, 340, 550, this));
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.PetBreakTxt_3, 680, 550, this));
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            this.onInfoUpdate();
            Dispatcher.addEventListener(EventName.PetBreakCopyEnter, this.onEnter);
            NetDispatcher.addCmdListener(ServerCommand.PetBreakCopyInfoRes, this.onInfoReq);
            NetDispatcher.addCmdListener(ServerCommand.PetBreakCopyInfoUpdate, this.onInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.PetAddOrRemove, this.onPetAddOrRemove);
            var _loc_3:* = Cache.instance.pet.fightPetUid;
            return;
        }// end function

        override public function hide() : void
        {
            super.hide();
            Dispatcher.removeEventListener(EventName.PetBreakCopyEnter, this.onEnter);
            NetDispatcher.removeCmdListener(ServerCommand.PetBreakCopyInfoRes, this.onInfoReq);
            NetDispatcher.removeCmdListener(ServerCommand.PetBreakCopyInfoUpdate, this.onInfoUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.PetAddOrRemove, this.onPetAddOrRemove);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            var _loc_1:* = Language.getArray(70330);
            this._tabBar = UIFactory.gTabBarNew(0, 32, _loc_1, 855, 580, 62, 26, this, this.onTabBarChange, "TabButtonNew");
            this._bg = UIFactory.gImageBitmap("", 8, 62, this);
            this._pointSp = UIFactory.sprite(8, 62, this);
            this._petListComBo = UIFactory.gComboBox(140, 550, 100, 20, PetBreakCopyUtil.petComboDp, this);
            this._petListComBo.prompt = "暂无宠物";
            this._sweep = UIFactory.gTextField("扫荡次数", 445, 550, 220, 20, this, GlobalStyle.textFormatHuang);
            this._sweep.mouseEnabled = false;
            this._vit = UIFactory.gTextField("体力", 750, 550, 220, 20, this, GlobalStyle.textFormatHuang);
            this._vit.mouseEnabled = false;
            this._addVitBtn = UIFactory.gLoadedButton(ImagesConst.Add_upSkin, 805, 553, 16, 16, this);
            this._addVitBtn.configEventListener(MouseEvent.CLICK, this.onClickAddVitBtn);
            LoaderHelp.addResCallBack(ResFileConst.petBreak, this.onResCompl);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._bg.dispose(param1);
            this._tabBar.dispose(param1);
            this._petListComBo.dispose(param1);
            this._sweep.dispose(param1);
            this._vit.dispose(param1);
            this._addVitBtn.dispose(param1);
            this.cleanPoint();
            UIFactory.disposeSprite(this._pointSp);
            if (this._effectPlayer)
            {
                this._effectPlayer.dispose(param1);
            }
            this._effectPlayer = null;
            this._openData = null;
            this._petListComBo = null;
            return;
        }// end function

    }
}
